package cn.sxt.logistics.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.sxt.logistics.mapper.OrderDetailMapper;
import cn.sxt.logistics.mapper.OrderMapper;
import cn.sxt.logistics.pojo.Order;
import cn.sxt.logistics.pojo.OrderDetail;
import cn.sxt.logistics.pojo.OrderDetailExample;
import cn.sxt.logistics.pojo.OrderDetailExample.Criteria;
import cn.sxt.logistics.pojo.OrderExample;
import cn.sxt.logistics.service.OrderService;


@Service
public class OrderServiceImpl implements OrderService {

	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	@Override
	public int deleteByPrimaryKey(Long orderId) {
		return orderMapper.deleteByPrimaryKey(orderId);
	}
	
	
	/*
	 * 新增订单【业务】有两个功能
	 * 1，向订单表插入数据
	 * 2，向订单明细表中插入数据
	 * 
	 */
	@Override
	public int insert(Order order) {
		
		// 1，向订单表插入数据
		int row = orderMapper.insert(order);
		
		//获取order订单表中的订单明细
		List<OrderDetail> orderDetails = order.getOrderDetails();
		for (OrderDetail orderDetail : orderDetails) {
			
			//将数据库生成的订单id设置给订单明细（订单明细数据属于当前订单）
			orderDetail.setOrderId(order.getOrderId());
			System.out.println(orderDetail);
			orderDetailMapper.insertSelective(orderDetail);
		}
		
		return row;
	}

	@Override
	public List<Order> selectByExample(OrderExample example) {
		return orderMapper.selectByExample(example);
	}
	
	/*
	 * 修改订单时候查询回显
	 * 1，先查询出订单基本信息
	 * 2，在根据订单的id去订单明细表查询出订单明细信息，
	 * 	并且封装到Order对象的orderDetail集合中
	 */
	@Override
	public Order selectByPrimaryKey(Long orderId) {
		Order order = orderMapper.selectByPrimaryKey(orderId);
		
		//根据订单id查询订单明细
		OrderDetailExample example = new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId);
		List<OrderDetail> orderDetails = orderDetailMapper.selectByExample(example );
		//将订单明细信息设置给订单对象
		order.setOrderDetails(orderDetails);
		
		return order;
	}
	/*
	 * 修改订单数据业务逻辑
	 * 1，修改订单基本信息
	 * 2，先删除订单明细表中当前订单的原有的数据
	 * 3.再将提交的数据插入到订单明细表中
	 * 
	 */
	@Override
	public int updateByPrimaryKeySelective(Order record) {
		//1，修改订单基本信息
		int row = orderMapper.updateByPrimaryKey(record);
		
		//2，先删除订单明细表中当前订单的原有的数据
		orderDetailMapper.deleteOrderDetailByOrderId(record.getOrderId());
		
		//3.再将提交的数据插入到订单明细表中
		//获取order订单表中的订单明细
		List<OrderDetail> orderDetails = record.getOrderDetails();
		for (OrderDetail orderDetail : orderDetails) {
			
			//将数据库生成的订单id设置给订单明细（订单明细数据属于当前订单）
			orderDetail.setOrderId(record.getOrderId());
			System.out.println(orderDetail);
			orderDetailMapper.insertSelective(orderDetail);
		}
		
		return row;
				
	}

	@Override
	public List<OrderDetail> selectOrderDetailsByOrderId(Long orderId) {
		
		OrderDetailExample example = new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId);
		List<OrderDetail> orderDetails = orderDetailMapper.selectByExample(example );
		return orderDetails;
	}


}
